<?php

include "../../dbaccess.php";
$connector = mysql_pconnect($dbserver, $dbuser, $dbpassword) or die(mysql_error($connector));
unset ($dbuser, $dbpassword, $dbserver);

mysql_select_db('gasilvis_EggenCards2') or die(mysql_error());

$CardKey= trim($_GET['CardKey']); 
$q= "SELECT CardImage FROM Card WHERE CardKey=".$CardKey;

$dataReturned = mysql_query($q) or die(mysql_error());

$row = mysql_fetch_array($dataReturned);  
if ($row)   $i= $row['CardImage']; 
else        die("no image");

$filename= isset($_GET['name']) ? $_GET['name'] : "card.jpg";

$orientation= isset($_GET['orientation']) ? $_GET['orientation'] : 0;

// update CardRef if there is request to do so
$first= true;
$onlyOrient= true;

// flip the card if necessary
if($orientation != 0) {
   switch($orientation) {
      case 1: // right 90 
         $i2 = imagecreatefromstring($i) ;
         $i3= imagerotate($i2, -90, 0);  
         $i= imagejpeg($i3);
         break;                  
      case 2: // left 90 
         $i2 = imagecreatefromstring($i) ;
         $i3= imagerotate($i2, 90, 0);
         $i= imagejpeg($i3);
         break;                  
      case 3: // 180 
         $i2 = imagecreatefromstring($i) ;
         $i3= imagerotate($i2, 180, 0);
         $i= imagejpeg($i3);
         break;                  
   }
}   
// can't get the saving of the card to work   
//   $q= "UPDATE Card SET CardImage='".mysql_real_escape_string(base64_encode($i))."' WHERE CardKey=".$CardKey;                                                                       
//   error_log($q);
//   mysql_query($q) or die(mysql_error());


if(isset($_GET['orientation'])) {
   $qOrient= ($first?" ":",")."Orientation='".trim($_GET['orientation'])."'";
   $first= false;
} else {
   $qOrient= "";
}

if(isset($_GET['note'])) {
   $x= trim($_GET['note']);
   $qNote= ($first?" ":",")."Note=\"".urldecode($x)."\"";
   $first= false;
   $onlyOrient= false;
} else {
   $qNote= "";
}

if(isset($_GET['class'])) {
   $qClass= ($first?" ":",")."Classification='".trim($_GET['class'])."'";
   $first= false;
   $onlyOrient= false;
} else {
   $qClass= "";
}

if(isset($_GET['color'])) {
   $qColor= ($first?" ":",")."Color='".trim($_GET['color'])."'";
   $first= false;
   $onlyOrient= false;
} else {
   $qColor= "";
}

if(!$onlyOrient) { // anything to update?
   $q= "UPDATE CardRef SET ".$qOrient.$qNote.$qClass.$qColor." WHERE CardKey=".$CardKey;  
   //error_log($q);                                                                     
   $qq= mysql_query($q) or die(mysql_error());
   file_put_contents("transaction.log", $q."\n", FILE_APPEND | LOCK_EX);
   //error_log($qq);

   // update User stats
   if(isset($_GET['echrs']) && isset($_GET['eccnt']) && isset($_GET['userid'])) {
      $echrs= trim($_GET['echrs']);
      $eccnt= trim($_GET['eccnt']);   
      $userid= trim($_GET['userid']);   
      $q= "UPDATE Users SET EChrs=".$echrs.", ECcnt=".$eccnt." WHERE UserID=".$userid;
      $qq= mysql_query($q) or die(mysql_error());
   }   
}                                               

//file_put_contents($filename, $i);
   
echo $i; // return card image

?>